package cn.always.xiajia.admin.sys.service.impl;

import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import cn.always.xiajia.admin.sys.entity.SysRole;
import cn.always.xiajia.admin.sys.entity.SysUserRole;
import cn.always.xiajia.admin.sys.mapper.SysRoleMapper;
import cn.always.xiajia.admin.sys.mapper.SysUserRoleMapper;
import cn.always.xiajia.admin.sys.service.SysRoleService;
import cn.always.xiajia.admin.sys.service.SysUserRoleService;
import cn.always.xiajia.framework.common.util.ExtCollUtil;
import cn.always.xiajia.framework.common.util.ExtCollectionUtils;
import cn.always.xiajia.framework.mybatis.core.service.impl.BaseServiceImplX;

@Service("sysRoleService")
public class SysRoleServiceImpl extends BaseServiceImplX<SysRoleMapper, SysRole> implements SysRoleService {

	@Resource
	protected SysUserRoleMapper sysUserRoleMapper;

	@Resource
	protected SysUserRoleService sysUserRoleService;

	@Resource
	protected SysRoleMapper sysRoleMapper;

	/**
	 * 通过用户获取角色
	 * 
	 * @param userId
	 * @return
	 */
	@Override
	public List<SysRole> getSysRoleByUserId(Long userId) {
		if (userId == null) {
			return Collections.emptyList();
		}
		Map<String, Object> param = new HashMap<>();
		param.put("EQ_userId", userId);
		List<Long> roleIds = ExtCollectionUtils.convertList(sysUserRoleService.xjList(param), SysUserRole::getRoleId);
		if (ExtCollUtil.isEmpty(roleIds)) {
			return Collections.emptyList();
		}

		param.clear();
		param.put("IN_roleId", roleIds);
		return xjList(param);
	}

	/**
	 * 通过角色id获取角色
	 * 
	 * @param roleId
	 * @return
	 */
	@Override
	public SysRole getSysRoleByRoleId(Long roleId) {
		return sysRoleMapper.selectById(roleId);
	}

	/**
	 * 通过角色ids获取角色
	 * 
	 * @param roleIds
	 * @return
	 */
	@Override
	public List<SysRole> getSysRoleByRoleIds(Collection<Long> roleIds) {
		if (ExtCollUtil.isEmpty(roleIds)) {
			return Collections.emptyList();
		}

		Map<String, Object> param = new HashMap<>();
		param.put("IN_roleId", roleIds);
		return xjList(param);
	}

}
